package com.cloudkinto.base;

import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.common.exception.BizException;
import com.cloudkinto.common.utils.dict.StaticDict;
import com.cloudkinto.inteceptors.RequestHolder;
import com.cloudkinto.session.UserSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @author zhangyy
 * @date 2020/4/16 14:16
 * @description:
 */
public class BaseController {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    public static UserSession getCurrentSession() {
        HttpServletRequest request = RequestHolder.getRequestLocal();
        if (null != request
                && null != request.getSession().getAttribute(UserSession.SESSION_ID_KEY)) {
            return (UserSession) request.getSession().getAttribute(UserSession.SESSION_ID_KEY);
        }
        return null;
    }

    public static Long getCurrentUserId() {
        HttpServletRequest request = RequestHolder.getRequestLocal();
        if (null != request && null != request.getSession().getAttribute(UserSession.SESSION_ID_KEY)) {
            return ((UserSession) request.getSession().getAttribute(UserSession.SESSION_ID_KEY)).getUserId();
        }
        throw new BizException(SysConstant.Session_Exception_UserId);
    }


    public static Long getCurrentUserIdNoEx() {
        HttpServletRequest request = RequestHolder.getRequestLocal();
        if (null != request && null != request.getSession().getAttribute(UserSession.SESSION_ID_KEY)) {
            return ((UserSession) request.getSession().getAttribute(UserSession.SESSION_ID_KEY)).getUserId();
        }
       return null;
    }
    public static Long getCompanyId() {
        HttpServletRequest request = RequestHolder.getRequestLocal();
        if (null != request && null != request.getSession().getAttribute(UserSession.SESSION_ID_KEY)) {
            return ((UserSession) request.getSession().getAttribute(UserSession.SESSION_ID_KEY)).getCompanyId();
        }
        return null;

    }

    public static Long getTenantId() {
        HttpServletRequest request = RequestHolder.getRequestLocal();
        if (null != request && null != request.getSession().getAttribute(UserSession.SESSION_ID_KEY)) {
            return ((UserSession) request.getSession().getAttribute(UserSession.SESSION_ID_KEY)).getTenantId();
        }
        return null;
    }

    public static Long getTenantId(String source) {
        HttpServletRequest request = RequestHolder.getRequestLocal();
        if (request == null) return null;
        UserSession session = (UserSession) request.getSession().getAttribute(UserSession.SESSION_ID_KEY);
        if (session == null) return null;
        if (!Objects.equals(session.getUserType(), StaticDict.System_RoleType.getRoleType(source))) {
            throw new BizException(SysConstant.Session_Exception_UserId);
        }
        return ((UserSession) request.getSession().getAttribute(UserSession.SESSION_ID_KEY)).getTenantId();

    }

    public static String getUserName() {
        HttpServletRequest request = RequestHolder.getRequestLocal();
        if (Objects.nonNull(request) && Objects.nonNull(request.getSession().getAttribute(UserSession.SESSION_ID_KEY))) {
            return ((UserSession) request.getSession().getAttribute(UserSession.SESSION_ID_KEY)).getUsername();
        }
        return null;
    }

    public static Long getWarehouseId() {
        HttpServletRequest request = RequestHolder.getRequestLocal();
        if (null != request && null != request.getSession().getAttribute(UserSession.SESSION_ID_KEY)) {
            return ((UserSession) request.getSession().getAttribute(UserSession.SESSION_ID_KEY)).getStorageId();
        }
        return null;
    }
}
